package com.hongxun.design.activitys;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hongxun.design.MyApp;
import com.hongxun.design.R;
import com.hongxun.design.adapters.RadioInterfaceAdapter;
import com.hongxun.design.bean.NextDoorBean;
import com.hongxun.design.interfaces.HeadSecond;
import com.hongxun.design.listeners.ListenerSet;
import com.hongxun.design.mytools.IconSourse;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.MyLog;
import com.hongxun.design.mytools.SetTypeface;
import com.hongxun.design.mytools.ToastShow;
import com.hongxun.design.ui.headandbottonpanel.HeadSecondControlPanel;
import com.hongxun.design.ui.ui_listview.Swipemenulistview.SwipeMenu;
import com.hongxun.design.ui.ui_listview.Swipemenulistview.SwipeMenuCreator;
import com.hongxun.design.ui.ui_listview.Swipemenulistview.SwipeMenuItem;
import com.hongxun.design.ui.ui_listview.Swipemenulistview.SwipeMenuListView;
import com.hongxun.design.utils.DataUtil;
import com.hongxun.design.utils.DensityUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zerdoor_pc on 2016/6/14.
 * RadioInterfaceActivity下户点单选界面
 */
public class RadioInterfaceActivity extends BaseActivity implements HeadSecond {
    int position;
    String title = "下户名";
    String type = "";
    private Handler handler;
    private final int StopR = 3;
    SwipeMenuListView radiointerface_list;
    List<NextDoorBean> data;
    RadioInterfaceAdapter adapter;
    RelativeLayout button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_radiointerface);
        findBar();
        findView();
        initialization();
    }

    @Override
    public void findBar() {
        Bundle bundle = getIntent().getExtras();
        position = bundle.getInt(MString.num);
        title = bundle.getString(MString.name);
        String LIST = bundle.getString(MString.LIST, "");
        String check = bundle.getString(MString.check, "");
        type = bundle.getString(MString.TYPE);
        //加载数据
        MyLog.L(MString.SUCCESSED + "LIST=" + LIST);
        MyLog.L(MString.SUCCESSED + "check=" + check);
        if (LIST.length() > 0) {
            data = new ArrayList<>();
            String[] tar = LIST.split(",");
            for (int i = 0; i < tar.length; i++) {
                if (tar[i] != null && tar[i].length() > 0) {
                    if (tar[i].equals(check)) {
                        data.add(new NextDoorBean(true, tar[i]));
                    } else {
                        data.add(new NextDoorBean(false, tar[i]));
                    }
                }
            }
        }

        HeadSecondControlPanel headsecond_layout = (HeadSecondControlPanel) findViewById(R.id.radiointerface_head);
        headsecond_layout.initHeadPanel(DataUtil.getsecondbean(MString.Save, View.VISIBLE, R.color.bar_right, ListenerSet.back, title, R.color.title, View.VISIBLE, IconSourse.BACK + "", View.VISIBLE));
        headsecond_layout.setClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                List<NextDoorBean> finish = adapter.getData();
                String s = "";
                String check = "";
                int i = 0;
                for (NextDoorBean n : finish) {
                    if (!(n.text.equals(getResources().getString(R.string.input_name)) || n.text.length() == 0)) {
                        if (n.is_check) {
                            s = n.text + "," + s;
                            check = n.text;
                            i++;
                        } else {
                            s += n.text + ",";
                            i++;
                        }
                    }
                }

                if (s.length() == 0) {
                    ToastShow.showShort(mActivity, "请至少输入一个下户名！");
                } else {
                    s = s.substring(0, s.length() - 1);
                    MyLog.L(MString.SUCCESSED + "s=" + s);
                    if (type.equals(MString.setpoint)) {
                        SetPointInformationActivity.adapter.setData(position, check, s, i);
                    } else {
                        DotDesignActivity.adapter.setData(position, check, s, i);
                    }
                    mActivity.finish();
                }


            }
        });
    }

    @Override
    protected void onResume() {
        MyApp.activity = this;
        mActivity = this;
        baseActivity = this;
        super.onResume();
    }

    @Override
    protected void findView() {
        super.findView();

        radiointerface_list = (SwipeMenuListView) findViewById(R.id.radiointerface_list);
//        radiointerface_list.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
//            @Override
//            public void onRefresh() {
//                radiointerface_list.setRefreshing(false);
//            }
//        });
        button = (RelativeLayout) findViewById(R.id.radiointerface_button);
        TextView textView = (TextView) findViewById(R.id.radiointerface_text);
        SetTypeface.set(textView, IconSourse.ADD);
        textView.append(" 新增");
        addHandler();
    }

    @Override
    protected void initialization() {
        super.initialization();
        if (data == null) {
            data = new ArrayList<>();
            data.add(new NextDoorBean(true, ""));
            data.add(new NextDoorBean(false, ""));
        }
        adapter = new RadioInterfaceAdapter(data, this);
        radiointerface_list.setAdapter(adapter);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                data.add(new NextDoorBean(false, ""));
                adapter.notifyDataSetChanged();
//                radiointerface_list.update();
            }
        });


        SwipeMenuCreator creator = new SwipeMenuCreator() {
            @Override
            public void create(SwipeMenu menu) {
                SwipeMenuItem deleteItem = new SwipeMenuItem(
                        getApplicationContext());
                deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
                        0x3F, 0x25)));
                deleteItem.setWidth(DensityUtil.dip2px(mActivity, 90));
                deleteItem.setIcon(R.mipmap.ic_delete);
                menu.addMenuItem(deleteItem);
            }
        };
        radiointerface_list.setMenuCreator(creator);
        radiointerface_list.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public void onMenuItemClick(int position, SwipeMenu menu, int index) {
                //删除方法
                data.remove(position);
                if (position == adapter.select) {
                    adapter.select = -1;
                }
                adapter.notifyDataSetChanged();
            }
        });
//        radiointerface_list.setMenuCreator(creator, new SwipeMenuListView.OnMenuItemClickListener() {
//            @Override
//            public void onMenuItemClick(int position, SwipeMenu menu, int index) {
//                //删除方法
//                data.remove(position);
//                if (position == adapter.select) {
//                    adapter.select = -1;
//                }
//                radiointerface_list.update();
//            }
//        });
    }

    private void addHandler() {
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case StopR:
                        break;

                }
            }
        };
    }
}
